home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.crl.com!tdd!dennis.weaver
- Distribution: world
- Newsgroups: comp.lang.c++
- Subject: Re: Borland C++ 4.5 : delete [] operator - what's goin
- From: dennis.weaver@datadim.com (Dennis Weaver)
- Message-ID: <1e8.123566.283@datadim.com>
- Date: Mon, 5 Feb 96 17:35:00 +0000
- Organization: TDD -- The Data Dimension -- Duluth, GA -- 404-495-9479
-
- History:
- >HI,
- >I'm using EasyWin (Borland C++ 4.5) to test my matrix library under
- Windows
- 3.1.
- >Could anybody tell me what's wrong in the following code?
- >
- >template<class T> class TMatrix
- >{
- > public:
- > TMatrix(size_t m = 0, size_t n = 0);
- > ~TMatrix()
- > {
- > delete[] elem;
- > delete[] pcol;
- > }
- > T& operator()(size_t i, size_t j);
- > {
- > return pcol[j][i];
- > }
- >
- > //....
- > private:
- > size_t nrow;
- > size_t ncol;
- > T** pcol; // pointer to columns
- > T* elem; // element array
- >};
- >
- >template<class T>
- >TMatrix<T>::TMatrix(size_e m, size_t n) : nrow(m), ncol(n)
- >{
- > elem = new T[m*n+1]; // a dummy space for efficient use
- > pcol = new T*[n+1]; // of 1-based indexing
- >
- > if (n > 0)
- > {
- > pcol[1] = elem;
- > for (int i=1; i <= n; i++)
- > pcol[i+1] = pcol[i] + m;
- > }
- >}
- >
- >typedef TMatrix<double> Matrix;
- >
- >void test()
- >{
- > Matrix a(4,4);
- > for (int i = 1; i <= 4; i++)
- > for (int j = 1; j <= 4; j++)
- > a(i,j) = i + j;
- >}
- >
- The destructor is going to delete the whole array, from index
- 0 through 4 (it doesn't know you're trying to make a 1-based
- index). Your pcol[i+1] = pcol[i] + m code did not initialize
- index 0 (you started with pcol[1] = elem). It looks like
- delete[] pcol is trying to free a NULL pointer at pcol[0].
- That's my best guess.
-